package com.chengyu.core.controller.shop;

import com.chengyu.core.component.OperationLog;
import com.chengyu.core.controller.AdminBaseController;
import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.form.UmsShopOwnLevelForm;
import com.chengyu.core.entity.CommonPage;
import com.chengyu.core.entity.CommonResult;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.UmsDeductionsDesignLog;
import com.chengyu.core.model.UmsShopLevel;
import com.chengyu.core.model.UmsShopLevelPrice;
import com.chengyu.core.model.UmsShopOwnLevel;
import com.chengyu.core.service.shop.ShopLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author LeGreen
 * @title 店铺
 * @date 2022/5/6
 */
@Api(tags = "店铺")
@Controller
@RequestMapping("/system")
public class ShopLevelController extends AdminBaseController {

    @Autowired
    private ShopLevelService shopLevelService;

    @ApiOperation(value = "店铺等级列表")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/getListShopLevel", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsShopLevel>> getList() throws ServiceException {

        List<UmsShopLevel> list = shopLevelService.listShopLevel(true, true, null, null);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "店铺等级价格列表")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/getListShopLevelPrice", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsShopLevelPrice>> getList(Integer levelId) throws ServiceException {

        List<UmsShopLevelPrice> list = shopLevelService.getShopLevelPriceByLevelId(levelId, null);
        return CommonResult.success(CommonPage.restPage(list));
    }


    @OperationLog
    @ApiOperation(value = "编辑添加店铺等级")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/editSubmitLevel", method = RequestMethod.POST)
    public CommonResult<String> editSubmit(UmsShopLevel umsShopLevel) throws ServiceException {
        if (umsShopLevel.getId() == null) {
            shopLevelService.addShopLevel(umsShopLevel);
        } else {
            shopLevelService.updateShopLevel(umsShopLevel);
        }
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "编辑添加店铺等级价格")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/editSubmitLevelPrice", method = RequestMethod.POST)
    public CommonResult<String> editSubmit(UmsShopLevelPrice umsShopLevelPrice) throws ServiceException {
        if (umsShopLevelPrice.getId() == null) {
            shopLevelService.addShopLevelPrice(umsShopLevelPrice);
        } else {
            shopLevelService.updateShopLevelPrice(umsShopLevelPrice);
        }
        return CommonResult.success(null);
    }


    @OperationLog
    @ApiOperation(value = "删除店铺等级")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/deleteShopLevel", method = RequestMethod.POST)
    public CommonResult<String> delete(String ids) throws ServiceException {
        if (StringUtils.isEmpty(ids)) {
            return CommonResult.failed("请至少选择一条数据");
        }
        for (String id : ids.split(CommonConstant.DH_REGEX)) {
            shopLevelService.deleteShopLevel(Integer.valueOf(id));
        }
        return CommonResult.success(null);
    }


    @OperationLog
    @ApiOperation(value = "删除店铺等级价格")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/deleteShopLevelPrice", method = RequestMethod.POST)
    public CommonResult<String> deleteShopLevelPrice(String ids) throws ServiceException {
        if (StringUtils.isEmpty(ids)) {
            return CommonResult.failed("请至少选择一条数据");
        }
        for (String id : ids.split(CommonConstant.DH_REGEX)) {
            shopLevelService.deleteShopLevelPrice(Integer.valueOf(id));
        }
        return CommonResult.success(null);
    }

    @OperationLog
    @ApiOperation(value = "查询店铺拥有的会员等级")
    @ResponseBody
    @RequestMapping(value = "/shopOwnLevel/selectByShopId", method = RequestMethod.POST)
    public CommonResult<List<UmsShopOwnLevelForm>> selectByShopId(Integer shopId) throws ServiceException {
        List<UmsShopOwnLevelForm> umsShopOwnLevelList = shopLevelService.getShopOwnLevelPriceById(shopId);
        return CommonResult.success(umsShopOwnLevelList);
    }

    @OperationLog
    @ApiOperation(value = "修改店铺拥有的会员等级")
    @ResponseBody
    @RequestMapping(value = "/shopOwnLevel/updateOwnLevel", method = RequestMethod.POST)
    public CommonResult<String> updateOwnLevel(UmsShopOwnLevel umsShopOwnLevel) throws ServiceException {
        shopLevelService.updateOwnLevel(umsShopOwnLevel);
        return CommonResult.success(null);
    }

    @ApiOperation(value = "获取商家会员拥有的权限-设计大于0的")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/getListShopOwnLevel", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsShopOwnLevelForm>> getListShopOwnLevel() throws ServiceException {

        List<UmsShopOwnLevelForm> list = shopLevelService.getShopOwnLevelList();
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "扣减设计数量")
    @ResponseBody
    @RequestMapping(value = "/shopLevel/deductions", method = RequestMethod.GET)
    public CommonResult<String> deductions(Integer type,Integer id) throws ServiceException {

        shopLevelService.deductions(type,id,getSessionAdmin().getName());
        return CommonResult.success(null);
    }

    @ApiOperation(value = "查看扣减记录")
    @ResponseBody
    @RequestMapping(value = "/deductions/deductionsList", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsDeductionsDesignLog>> deductionsList() throws ServiceException {

        List<UmsDeductionsDesignLog> list = shopLevelService.deductionsList();
        return CommonResult.success(CommonPage.restPage(list));
    }
}